$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'baseinfo/lhpequipment/list',
        datatype: "json",
        colModel: [
			{ label: '设备名称', name: 'equipmentName', index: 'equipment_name', width: 80 },
			{ label: '安装点', name: 'pointName', index: 'point_id', width: 80 },
			{ label: '厂家', name: 'manufactorType', index: 'manufactor_type', width: 80 ,formatter: function(value, options, row) {
                    if (value == 0) {
                        return '维萨拉';
                    } else if (value == 1) {
                        return '路赋特';
                    }
                        return value;
                }},
            { label: '设备类型', name: 'equipmentType', index: 'equipment_type', width: 80,formatter: function(value, options, row) {
                    if (value == 0) {
                        return '组合';
                    } else if (value == 1) {
                        return '能见度传感器';
                    } else if (value == 2) {
                        return '气象站';
                    }else if (value == 3) {
                        return '被动式路面传感器';
                    }
                        return value;
                } },
			{ label: '设备状态', name: 'equipmentStatus', index: 'equipment_status', width: 80 ,formatter: function(value, options, row){
                    return value == 0 ?
                        '<span style="color: #00ca6d">在线</span>' :
                        '<span style="color:red">离线</span>';
                }},
            { label: '是否在用', name: 'isActive', index: 'is_active', width: 80 ,formatter: function(value, options, row){
                    return value == 0 ?
                        '正常' :
                        '禁用';
                }},
			{ label: '安装时间', name: 'installTime', index: 'install_time', width: 80 }			
        ],
		viewrecords: true,
        height: 268,
        rowNum: 10,
		rowList : [10,30,50],
       // rownumbers: true,
        rownumWidth: 35,
        autowidth:true,
        //multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{
            equipmentName:null,
            status:""
        },
		lhpEquipment: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.lhpEquipment = {
			    pointId:null,
                pointName:null,
                isActive:0
            };

			vm.getPoint();
		},
        getPoint: function(){
            //加载路段树
            $.get(baseURL + "baseinfo/lhpinstallationpoint/pointTree", function(r){
                ztree = $.fn.zTree.init($("#pointTree"), setting, r);
                var node = ztree.getNodeByParam("id", vm.lhpEquipment.pointId);


                if(node != null){
                    ztree.selectNode(node);
                    vm.lhpEquipment.pointName = node.name;
                }
            })
        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                vm.lhpEquipment.installTime=$("#installTime").val();
                var url = vm.lhpEquipment.id == null ? "baseinfo/lhpequipment/save" : "baseinfo/lhpequipment/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.lhpEquipment),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "baseinfo/lhpequipment/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "baseinfo/lhpequipment/info/"+id, function(r){
                vm.lhpEquipment = r.lhpEquipment;
                vm.getPoint();
            });
		},
        pointTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择安装点",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#pointLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.lhpEquipment.pointId = node[0].id;
                    vm.lhpEquipment.pointName = node[0].name;
                    layer.close(index);
                }

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'equipmentName': vm.q.equipmentName,
                    "equipmentStatus":vm.q.status
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});